package services

import (
	"go.mongodb.org/mongo-driver/bson/primitive"
	"imooc-product/datamodels"
	"imooc-product/repositories"
)

type IProductService interface {
	GetProductById(primitive.ObjectID)(*datamodels.Product,error)
	GetAllProduct()([]*datamodels.Product,error)
	DeleteProductByID(primitive.ObjectID)bool
	InsertProduct(*datamodels.Product)(interface{},error)
	UpdateProduct(*datamodels.Product)error
}

type ProductService struct {
	productRepository repositories.IProduct
}

func NewProductService(repository repositories.IProduct)IProductService  {
	return &ProductService{repository}
}

func (p *ProductService)GetProductById(productid primitive.ObjectID)(*datamodels.Product,error)  {
	return p.productRepository.SelectByKey(productid)
}
func (p *ProductService)GetAllProduct()([]*datamodels.Product,error)  {
	return p.productRepository.SelectAll()
}
func (p *ProductService)DeleteProductByID(productid primitive.ObjectID)bool  {
	return p.productRepository.Delete(productid)
}
func (p *ProductService)InsertProduct(product *datamodels.Product)(interface{},error)  {
	return p.productRepository.Insert(product)
}
func (p *ProductService)UpdateProduct(product *datamodels.Product)error  {
	return p.productRepository.Update(product)
}
